This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
source("tianfengRwrappers.R")
umapplot(ds2)
ds2 <- RenameIdents(ds2,"FB" = "Fibroblast", "pericyte" = "Pericyte","fibromyocyte"="Fibromyocyte")
umapplot(ds2, group.by = "seurat_clusters")
Idents(ds2) <- factor(Idents(ds2),levels =
c("SMC1","Fibromyocyte","Pericyte","Fibroblast","SMC2"))
umapplot(ds2)
ds2$Classification1 <- Idents(ds2)
saveRDS(ds2,"ds2.rds")
ggsave("./fig3/ds2.svg",plot = umapplot(ds2),device = svg, width = 6, height = 5)
ds0 <- readRDS("ds0.rds")
multi_featureplot(c("FABP4", "RERGL", "NRIP2","HIGD1B"),ds2,labels = "")
viotheme <- theme(plot.title = element_text(size = 17,color="black",hjust = 0.5),
axis.title = element_text(size = 17,color ="black"),
axis.text = element_text(size = 17,color = "black"),
panel.grid.minor.y = element_blank(),
panel.grid.minor.x = element_blank(),
axis.text.x = element_text(size= 17, angle = 0),
panel.grid=element_blank(),
legend.position = "top",
legend.text = element_text(size= 17),
legend.title= element_text(size= 17))
stat_theme <- stat_compare_means(aes(group = sample),
label = "p.format",
method = "wilcox.test", size = 6,
label.y = max(merge_expr$expr),
hide.ns = F)
ds2FbM <- subset(ds2,Classification1 == "Fibromyocyte")
ds1FbM <- subset(ds1,ref_celltype == "Fibromyocyte")
ds2data <- get_data_table(ds2FbM,type = "data")
ds1data <- get_data_table(ds1FbM,type = "data")
func1 <- function(gene, sample, datable){
data.frame(expr = datable[gene,], sample = sample, gene = gene)
}
merge_expr <- data.frame()
for (i in lapply(genes_to_show, func1,"ds1",ds1data))
{
merge_expr <- rbind(merge_expr,i)
}
for (i in lapply(genes_to_show, func1,"ds2",ds2data))
{
merge_expr <- rbind(merge_expr,i)
}
rownames(merge_expr) <- NULL
Data_summary <- Rmisc::summarySE(merge_expr, measurevar="expr", groupvars=c("sample","gene"))
# head(Data_summary)
ggobj <- ggplot(merge_expr,aes(x = gene, y = expr,fill = sample)) +
geom_split_violin(trim= F, color="white", scale = "area") +
geom_point(data = Data_summary,aes(x = gene, y= expr), pch=19,
position=position_dodge(0.2),size= 1) + #绘制均值位置
geom_errorbar(data = Data_summary, aes(ymin = expr-ci, ymax= expr+ci),
width= 0.05,
position= position_dodge(0.2), #误差线位置,和均值位置相匹配
color="black",
alpha = 0.7,
size= 0.5) +
scale_fill_manual(values = c("#b1d6fb", "#fd9999"))+
labs(y=("gene expression"),x=NULL, title = "Split violin") +
theme_classic()+ viotheme + stat_theme
ggobj
ggsave("./fig3/supds2tods1.png", device = png, plot = ggobj, height = 5, width = 7)
# Idents(ds0) <- ds0$ref_celltype
# umapplot(ds0,group.by = "ref_celltype")
ds2FbM <- subset(ds2, Classification1 == "Fibromyocyte")
ds0FbM <- subset(ds0, ref_celltype == "Fibromyocyte")
ds2data <- get_data_table(ds2FbM,type = "data")
ds0data <- get_data_table(ds0FbM,type = "data")
merge_expr <- data.frame()
for (i in lapply(genes_to_show, func1,"ds0",ds0data))
{
merge_expr <- rbind(merge_expr,i)
}
for (i in lapply(genes_to_show, func1,"ds2",ds2data))
{
merge_expr <- rbind(merge_expr,i)
}
rownames(merge_expr) <- NULL
Data_summary <- Rmisc::summarySE(merge_expr, measurevar="expr", groupvars=c("sample","gene"))
head(Data_summary)
ggobj <- ggplot(merge_expr,aes(x = gene, y = expr,fill = sample)) +
geom_split_violin(trim= F, color="white", scale = "area") +
geom_point(data = Data_summary,aes(x = gene, y= expr), pch=19,
position=position_dodge(0.2),size= 1) + #绘制均值位置
geom_errorbar(data = Data_summary, aes(ymin = expr-ci, ymax= expr+ci),
width= 0.05,
position= position_dodge(0.2), #误差线位置,和均值位置相匹配
color="black",
alpha = 0.7,
size= 0.5) +
scale_fill_manual(values = c("#b1d6fb", "#fd9999"))+
labs(y=("gene expression"),x=NULL,title = "Split violin") +
theme_classic()+ viotheme + stat_theme
ggobj
ggsave("./fig3/supds2tods0.png", device = png, plot = ggobj, height = 5, width = 7)
viotheme <- theme(plot.title = element_text(size = 12,color="black",hjust = 0.5),
axis.title = element_text(size = 12,color ="black"),
axis.text = element_text(size= 12,color = "black"),
panel.grid.minor.y = element_blank(),
panel.grid.minor.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1 ),
panel.grid=element_blank(),
legend.position = "top",
legend.text = element_text(size= 12),
legend.title= element_text(size= 12))
# https://stackoverflow.com/a/45614547
GeomSplitViolin <- ggproto("GeomSplitViolin", GeomViolin, draw_group = function(self, data, ..., draw_quantiles = NULL){
data <- transform(data, xminv = x - violinwidth * (x - xmin), xmaxv = x + violinwidth * (xmax - x))
grp <- data[1,'group']
newdata <- plyr::arrange(transform(data, x = if(grp%%2==1) xminv else xmaxv), if(grp%%2==1) y else -y)
newdata <- rbind(newdata[1, ], newdata, newdata[nrow(newdata), ], newdata[1, ])
newdata[c(1,nrow(newdata)-1,nrow(newdata)), 'x'] <- round(newdata[1, 'x'])
if (length(draw_quantiles) > 0 & !scales::zero_range(range(data$y))) {
stopifnot(all(draw_quantiles >= 0), all(draw_quantiles <=
1))
quantiles <- ggplot2:::create_quantile_segment_frame(data, draw_quantiles)
aesthetics <- data[rep(1, nrow(quantiles)), setdiff(names(data), c("x", "y")), drop = FALSE]
aesthetics$alpha <- rep(1, nrow(quantiles))
both <- cbind(quantiles, aesthetics)
quantile_grob <- GeomPath$draw_panel(both, ...)
ggplot2:::ggname("geom_split_violin", grid::grobTree(GeomPolygon$draw_panel(newdata, ...), quantile_grob))
}
else {
ggplot2:::ggname("geom_split_violin", GeomPolygon$draw_panel(newdata, ...))
}
})
geom_split_violin <- function (mapping = NULL, data = NULL, stat = "ydensity", position = "identity", ..., draw_quantiles = NULL, trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) {
layer(data = data, mapping = mapping, stat = stat, geom = GeomSplitViolin, position = position, show.legend = show.legend, inherit.aes = inherit.aes, params = list(trim = trim, scale = scale, draw_quantiles = draw_quantiles, na.rm = na.rm, ...))
}
scatter_theme2 <-
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
ref_ds2FBM <- subset(ds2,Classification1 == "Fibromyocyte")
unaligned_ds0FBM <- subset(ds0,Classification1 == "Fibromyocyte")
harmony_ds0FbM <- subset(CAD_merge_harmony, orig.ident == "ds0" & ds2_celltype == "Fibromyocyte") #harmony
CCA_ds0FbM <- subset(CAD_merge_CCA, orig.ident == "ds0" & ds2_celltype == "Fibromyocyte")
xgb_ds0FbM <- subset(ds0, ref_celltype == "Fibromyocyte")
ggplot(data, aes(x=LUM, y=BGN, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
ggplot(data, aes(x=LUM, y=ACTA2, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
ggplot(data, aes(x=LUM, y=ACTA2, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
ggplot(data, aes(x=TAGLN, y=ACTA2, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
data1 <- FetchData(object = xgb_ds0FbM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data1) <- NULL
data1$group <- "xgb_ds0"
data2 <- FetchData(object = CCA_ds0FbM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data2) <- NULL
data2$group <- "CCA_ds0"
data3 <- FetchData(object = unaligned_ds0FBM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data3) <- NULL
data3$group <- "unaligned_ds0"
data4 <- FetchData(object = ref_ds2FBM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data4) <- NULL
data4$group <- "ref_ds2"
data <- rbind(data1,data2,data3,data4)
ggplot(data, aes(x=LUM, y=BGN, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
ggplot(data, aes(x=LUM, y=ACTA2, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
ggplot(data, aes(x=TAGLN, y=ACTA2, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
ref_ds2FBM <- subset(ds2,Classification1 == "Fibromyocyte")
unaligned_ds1FBM <- subset(ds1,Classification1 == "Fibromyocyte")
harmony_ds1FbM <- subset(CAD_merge_harmony, orig.ident == "ds1" & ds2_celltype == "Fibromyocyte") #harmony
CCA_ds1FbM <- subset(CAD_merge_CCA, orig.ident == "ds1" & ds2_celltype == "Fibromyocyte")
xgb_ds1FbM <- subset(ds1, ref_celltype == "Fibromyocyte")
data1 <- FetchData(object = xgb_ds1FbM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data1) <- NULL
data1$group <- "xgb_ds1"
data2 <- FetchData(object = harmony_ds1FbM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data2) <- NULL
data2$group <- "Harmony_ds1"
data3 <- FetchData(object = ref_ds2FBM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data3) <- NULL
data3$group <- "ref_ds2"
data <- rbind(data1,data2,data3)
ggplot(data, aes(x=LUM, y=BGN, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
ggplot(data, aes(x=LUM, y=ACTA2, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
ggplot(data, aes(x=TAGLN, y=ACTA2, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
data1 <- FetchData(object = xgb_ds1FbM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data1) <- NULL
data1$group <- "xgb_ds1"
data2 <- FetchData(object = CCA_ds1FbM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data2) <- NULL
data2$group <- "CCA_ds1"
data3 <- FetchData(object = unaligned_ds1FBM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data3) <- NULL
data3$group <- "unaligned_ds1"
data4 <- FetchData(object = ref_ds2FBM, vars = c("LUM", "ACTA2","BGN","TAGLN"))
rownames(data4) <- NULL
data4$group <- "ref_ds2"
data <- rbind(data1,data2,data3,data4)
ggplot(data, aes(x=LUM, y=BGN, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
ggplot(data, aes(x=LUM, y=ACTA2, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
ggplot(data, aes(x=TAGLN, y=ACTA2, color = group, group = group)) +
geom_point(size = 3,alpha = 0.1) +
geom_smooth(method=lm , se=TRUE) +
theme_classic() + theme(axis.title = element_text(size = 20,color = "black"),
axis.text = element_text(size = 20,color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
title = element_text(size = 20))
`geom_smooth()` using formula 'y ~ x'
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.